﻿using System.Runtime.InteropServices;

public static class _API
{
    [DllImport("Kernel32.dll", SetLastError = true)]
    private static extern bool SetSystemTime(ref SYSTEMTIME time);


    [DllImport("Kernel32.dll", SetLastError = true)]
    private static extern bool SetLocalTime(ref SYSTEMTIME lpSystemTime);

    private struct SYSTEMTIME
    {
        public short Year;
        public short Month;
        public short DayOfWeek;
        public short Day;
        public short Hour;
        public short Minute;
        public short Second;
        public short Milliseconds;
    }

    /// <summary>
    /// Sets the date and time on the system.
    /// </summary>
    /// <param name="dt"></param>
    public static void SetLocalTime(System.DateTime dt)
    {
        SYSTEMTIME time;
        time.Year = (short)dt.Year;
        time.Month = (short)dt.Month;
        time.Day = (short)dt.Day;
        time.Hour = (short)dt.Hour;
        time.Minute = (short)dt.Minute;
        time.Second = (short)dt.Second;
        time.Milliseconds = (short)dt.Millisecond;
        time.DayOfWeek = (short)dt.DayOfWeek;

        SetLocalTime(ref time);
    }

    /// <summary>
    /// Sets the current system time and date. 
    /// The system time is expressed in Coordinated Universal Time (UTC).
    /// </summary>
    /// <param name="dt"></param>
    public static void SetSystemTime(System.DateTime dt)
    {
        SYSTEMTIME time;
        time.Year = (short)dt.Year;
        time.Month = (short)dt.Month;
        time.Day = (short)dt.Day;
        time.Hour = (short)dt.Hour;
        time.Minute = (short)dt.Minute;
        time.Second = (short)dt.Second;
        time.Milliseconds = (short)dt.Millisecond;
        time.DayOfWeek = (short)dt.DayOfWeek;

        SetSystemTime(ref time);
    }
}
